package com.s.core.root.strategy.base;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Optional;

public abstract class FeatureContainer {

    private final List<Feature> featureList = Lists.newArrayList();

    public void addFeature(Feature feature) {
        if (featureList.stream().anyMatch(f -> f.getClass() == feature.getClass())) {
            throw new RuntimeException();
        }
        featureList.add(feature);
    }

    public boolean containFeature(Class<? extends Feature> featureClass) {
        return featureList.stream().anyMatch(f -> f.getClass() == featureClass);
    }

    @SuppressWarnings("unchecked")
    public <T extends Feature> T getFeature(Class<T> featureClass) {
        Optional<Feature> featureOptional = featureList.stream().
                filter(f -> f.getClass() == featureClass).findFirst();
        if (featureOptional.isPresent()) {
            return (T) featureOptional.get();
        }
        throw new RuntimeException();
    }
}
